package xx.iverson.file_manager.util;

import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;

/**
 * xx.iverson.file_manager.util.GetLocalIP
 * 描述：
 * on 2025/9/26-下午5:14
 *
 * @author weijx
 */
@Slf4j
public class GetLocalIP {
    public static void main(String[] args) throws Exception {
        System.out.println(getLocalIp());
    }
    public static String getLocalIp() throws Exception {
        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();

        while (interfaces.hasMoreElements()) {
            NetworkInterface networkInterface = interfaces.nextElement();

            if (networkInterface.isLoopback() || !networkInterface.isUp()) {
                continue;
            }

            Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();
            while (addresses.hasMoreElements()) {
                InetAddress address = addresses.nextElement();

                if (address instanceof Inet4Address) {
                    return address.getHostAddress();
                }
            }
        }

        return "无法获取IP地址";
    }
}
